package com.oa.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CertificationBodySaveDTO;
import com.oa.admin.entity.AuthenticationStepFile;
import com.oa.admin.entity.AuthenticationSteps;
import com.oa.admin.service.IAuthenticationStepFileService;
import com.oa.admin.service.IAuthenticationStepsService;
import com.oa.admin.vo.CertificationBodyDetailsVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.CertificationBody;
import com.oa.admin.mapper.CertificationBodyMapper;
import com.oa.admin.service.ICertificationBodyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 认证机构 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class CertificationBodyServiceImpl extends ServiceImpl<CertificationBodyMapper, CertificationBody> implements ICertificationBodyService {

    private final CertificationBodyMapper certificationBodyMapper;

    @Autowired
    public CertificationBodyServiceImpl(CertificationBodyMapper certificationBodyMapper) {
        this.certificationBodyMapper = certificationBodyMapper;
    }

    @Autowired
    private IAuthenticationStepsService authenticationStepsService;

    @Autowired
    private IAuthenticationStepFileService authenticationStepFileService;

    @Override
    @CommonService
    public PageResult<CertificationBody> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<CertificationBody> list = certificationBodyMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<CertificationBody> page = new Page<>(pageNum, pageSize);
            //查询
            List<CertificationBody> list = certificationBodyMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public CertificationBody findCertificationBodyById(String id){
        return certificationBodyMapper.findById(id);
    }

    @CommonService
    @Override
    public CertificationBodyDetailsVO findCertificationBodyDetails(String id) {
        return certificationBodyMapper.findCertificationBodyDetails(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(CertificationBodySaveDTO certificationBodySaveDTO) {
        CertificationBody certificationBody = new CertificationBody();
        BeanUtils.copyProperties(certificationBodySaveDTO, certificationBody);
        certificationBody.setId(null);
        if(insert(certificationBody)){

            if(certificationBodySaveDTO.getAuthenticationSteps() != null && certificationBodySaveDTO.getAuthenticationSteps().size() > 0){
                certificationBodySaveDTO.getAuthenticationSteps().forEach(d -> {
                    AuthenticationSteps authenticationSteps = new AuthenticationSteps();
                    BeanUtils.copyProperties(d, authenticationSteps);
                    authenticationSteps.setCertificationId(certificationBody.getId());
                    authenticationStepsService.insert(authenticationSteps);

                    if(d.getAuthenticationStepFileAddDTO() != null && d.getAuthenticationStepFileAddDTO().size() > 0){
                        List<AuthenticationStepFile> authenticationStepFiles = new ArrayList<>();
                        d.getAuthenticationStepFileAddDTO().forEach(a -> {
                            AuthenticationStepFile authenticationStepFile = new AuthenticationStepFile();
                            BeanUtils.copyProperties(a, authenticationStepFile);
                            authenticationStepFile.setAuthenticationStepId(authenticationSteps.getId());
                            authenticationStepFiles.add(authenticationStepFile);
                        });
                        authenticationStepFileService.insertBatch(authenticationStepFiles);
                    }
                });
            }
            return new Resp().success("添加成功");
        }
        return new Resp().failure("添加失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp edit(CertificationBodySaveDTO certificationBodySaveDTO) {
        CertificationBody certificationBody = new CertificationBody();
        BeanUtils.copyProperties(certificationBodySaveDTO, certificationBody);
        if(updateById(certificationBody)){
            if(certificationBodySaveDTO.getAuthenticationSteps() != null && certificationBodySaveDTO.getAuthenticationSteps().size() > 0){
                authenticationStepFileService.delete(new EntityWrapper().where("authentication_step_id = (select id from admin_authentication_steps where certification_id = {0})", certificationBody.getId()));
                authenticationStepsService.delete(new EntityWrapper().eq("certification_id", certificationBody.getId()));
                certificationBodySaveDTO.getAuthenticationSteps().forEach(d -> {
                    AuthenticationSteps authenticationSteps = new AuthenticationSteps();
                    BeanUtils.copyProperties(d, authenticationSteps);
                    authenticationSteps.setCertificationId(certificationBody.getId());
                    authenticationStepsService.insert(authenticationSteps);

                    if(d.getAuthenticationStepFileAddDTO() != null && d.getAuthenticationStepFileAddDTO().size() > 0){
                        List<AuthenticationStepFile> authenticationStepFiles = new ArrayList<>();
                        d.getAuthenticationStepFileAddDTO().forEach(a -> {
                            AuthenticationStepFile authenticationStepFile = new AuthenticationStepFile();
                            BeanUtils.copyProperties(a, authenticationStepFile);
                            authenticationStepFile.setAuthenticationStepId(authenticationSteps.getId());
                            authenticationStepFiles.add(authenticationStepFile);
                        });
                        authenticationStepFileService.insertBatch(authenticationStepFiles);
                    }

                });
            }
            return new Resp().success("修改成功");
        }
        return new Resp().failure("修改失败");
    }
}
